﻿using System;
using GraphProcessor;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Logic;
using UnityEditor;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay.Editors
{
    [System.Serializable, NodeMenuItem(LevelEditorConst.Dynamic, typeof(LevelServiceGraph))]
    public class LevelServiceTargetSelectNode : LevelServiceBaseNode
    {
        public override Color color => new Color(0.9f, 0.7f, 0.1f, 1);

        public override string name => LevelEditorConst.Dynamic;
        [Input("执行", false)] public EditorLevelServiceTargetSetter Setter;

        [LevelServiceEditorCustomProperty("Ta", "类型")]
        public int paramType;
        [LevelServiceEditorCustomProperty("Ta", "EntityID"), Tooltip("自行在LevelEntityModuleId表及LevelEntityTag找相应ID配置或是关卡编辑器中进行配置")]
        public int entityID;
        [LevelServiceEditorCustomProperty("Ta", "动态Key")]
        public string key;

        protected override void Process()
        {
            base.Process();
            if (this.Setter == null)
            {
                var rGraphPath = AssetDatabase.GetAssetPath(this.graph);
                LogGame.LogError($"【{rGraphPath}】中节点【{this.name}】未连接原节点");
                return;
            }
            this.Setter.setter?.Invoke(this.CreateConfig());
        }

        protected EditorLevelServiceDynamicConfig CreateConfig()
        {
            return new EditorLevelServiceDynamicConfig()
            {
                paramType = (byte)this.paramType,
                paramInt = this.entityID,
                paramStr = this.key
            };
        }

        public void Sync(EditorLevelServiceDynamicConfig rConfig)
        {
            this.paramType = rConfig.paramType;
            this.entityID = rConfig.paramInt;
            this.key = rConfig.paramStr;
        }
    }
    public class EditorLevelServiceTargetSetter
    {
        public Action<EditorLevelServiceDynamicConfig> setter;

        public EditorLevelServiceTargetSetter(Action<EditorLevelServiceDynamicConfig> setter)
        {
            this.setter = setter;
        }
    }
}